<script>
import $config from '@/config';
import { mapState } from 'vuex';

export default {
  globalData: {
    os: 'ios',
    commentConfigInfoItem: {
      // 评论插件需要的信息
      api: {
        host: $config.serviceApi,
        prefix: ''
      },
      appid: $config.appId
    },
    CommentRequireInfo: {} // 评论商品信息
  },
  computed: {
    ...mapState({
      userInfo: state => state['user'].userInfo,
      clearStoreStorage: state => state['pages'].clearStoreStorage
    })
  },
  onLaunch() {
    try {
      if (!this.userInfo?.nickname) {
        this.$store.dispatch('user/login');
      }
    } catch (e) {
      console.warn('app onLaunch-> ', e);
    }
    // console.log('onLaunch-> ', res);
    // 配置展示我的界面中订阅消息浮层
    this.$store.dispatch('app/setData', {
      showMySubscription: true
    });
    // 小程序每次热启动时重置首页tab选中状态
    uni.setStorageSync('HOME-TABS-ACTIVE', 'walk');

    // 校验设备，如果是pc端，那么跳转到错误界面
    // setTimeout(() => {
      uni.getSystemInfo({
        success: (res) => {
          if (['windows', 'mac'].includes(res.platform)) {
            uni.reLaunch({
              url: '/pages/error/index'
            });
          }
        }
      });
    // }, 1000);
  },
  onShow(res) {
    // console.log('APP onShow-> ', res);
    this.$store.dispatch('pages/setData', {
      scene: res.scene
    });
    try {
      if (res.query && res.query.messageTemplateId) {
        // 缓存通过订阅消息进入小程序时的数据，供后期下单时使用
        uni.setStorageSync('MESSAGE-TEMPLATE-ID', res.query.messageTemplateId);
      }

      // 查询本地是否有未上报成功的日志数据，再次执行一下
      // setTimeout(() => {
        const storageViewAttrCatch = uni.getStorageSync('storage-view-attr-catch');
        if (storageViewAttrCatch) {
          this.$store.dispatch('pages/fetchReportViewAttr', storageViewAttrCatch).then(() => {
            uni.removeStorageSync('storage-view-attr-catch');
          });
        }
      // }, 2000);
      if (this.userInfo?.nickname) {
        this.$http({
          login: true,
          url: '/order/app/order/star/count',
          method: 'get',
          report: true
        }).then(res => {
          const data = res.data;
          if (data) {
            let count = 0;
            for (const key in data) {
              if (data.hasOwnProperty(key)) {
                count += data[key];
              }
            }
            this.$store.dispatch('pages/setData', {
              orderStatusInfo: data
            });
            if (count > 0) {
              uni.showTabBarRedDot({ // 显示订单红点
                index: 1 // tabbar下标
              });
              uni.showTabBarRedDot({ // 显示我的红点
                index: 2 // tabbar下标
              });
            } else {
              uni.hideTabBarRedDot({
                index: 1 // tabbar下标
              });
              uni.hideTabBarRedDot({
                index: 2 // tabbar下标
              });
            }
          }
        }).catch(err => {
          console.log(err, 22222222);
        });
      }
    } catch (e) {
      console.warn('app onShow-> ', e);
    }
  },
  onHide() {
    // console.log('APP onHide-> ');
    try {
      // 小程序进入后台后，清除定向弹出层部分缓存
      const storageActivityDetail = uni.getStorageSync('storage-activity-detail');
      if (storageActivityDetail) {
        storageActivityDetail['show-list'] = [];
        uni.setStorageSync('storage-activity-detail', storageActivityDetail);
      }
    } catch (e) {
      console.warn('APP onHide->', e);
    }
    if (this.clearStoreStorage) {
      // 更新store访问数据，投放中转页使用
      // this.$store.dispatch('pages/fetchAppStore');
      this.$store.dispatch('message/setData', {
        dialogSubscribeGuide: false
      });
    }
  },
  onPageNotFound() {
    uni.switchTab({
      url: '/pages/index/index'
    });
  },
  onError(err) {
    try {
      this.$store.dispatch('app/setAlarm', {
        code: 'AppOnError',
        message: err,
        url: 'onError',
        requestData: {},
        header: {}
      });
    } catch (e) {
      console.warn('onError-> ', e);
    }
  }
};
</script>

<style lang="scss">
  ::-webkit-scrollbar{
    width: 0;
    height: 0;
    background-color: transparent;
  }
  page{ background-color: $uni-page-bg;  color:$uni-color-default}
  image{max-width: 100%; vertical-align: middle;}
  view,
  text,
  icon,
  button,
  input,
  swiper,
  swiper-item,
  scroll-view,
  navigator {box-sizing:border-box;color: inherit;font: inherit;height:initial;}
  .flex{ display: flex;}
  .inline-block{ display: inline-block;}
  .safe-area{
    padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .safe-area-inset-bottom{ height: env(safe-area-inset-bottom);}
  .center{
    align-items: center;
    justify-content: center;
  }
  .flex{
    display: flex;
  }
  .flex1{
    flex: 1;
  }
  .text-decoration{
    text-decoration:line-through;
  }
  .align-center{
    align-items: center;
  }
  ._red{
    color: $uni-color-primary;
  }
  .color-777 {
    color: #777777;
  }

  ._blod{
    font-weight: bold;
  }
  .font-20{ font-size: 20px; }
  .font-22{ font-size: 22px; }
  .font-24{ font-size: 24px; }
  .font-26{ font-size: 26px; }
  .font-28{ font-size: 28px; }
  .font-30{ font-size: 30px; }
  .font-32{ font-size: 32px; }
  .font-34{ font-size: 34px; }
  .font-36{ font-size: 36px; }
  .margin-rt-6{
    margin-right: 6px;
  }
  .line-clamp-1{
    -webkit-line-clamp: 1;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
  }
  .line-clamp-2{
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
  }

  .x-grid{
    display: grid;
    &.span-1{ grid-template-columns: repeat(1, minmax(0px, 1fr));}
    &.span-2{ grid-template-columns: repeat(2, minmax(0px, 1fr));}
    &.span-3{ grid-template-columns: repeat(3, minmax(0px, 1fr));}
    &.span-4{ grid-template-columns: repeat(4, minmax(0px, 1fr));}
    &.span-5{ grid-template-columns: repeat(5, minmax(0px, 1fr));}
    &.span-6{ grid-template-columns: repeat(6, minmax(0px, 1fr));}
    &.span-7{ grid-template-columns: repeat(7, minmax(0px, 1fr));}
    &.span-8{ grid-template-columns: repeat(8, minmax(0px, 1fr));}
    &.span-9{ grid-template-columns: repeat(9, minmax(0px, 1fr));}
  }
</style>
